home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1994 / MacHack 1994.toast / MacHack™ 1987-1994 / MacHack™ '87 / Source ƒ.sea / Source ƒ / Pascal ƒ / Pascal utils / Utility Documentation / Utility Documentation
Encoding:
Text File  |  1986-12-09  |  14.1 KB  |  339 lines  |  [TEXT/QED1]

  1.     The Following is the Interface part of a Utilities Unit written by
  2. David O'Rourke.  The purpose is to provide some general utilities that will
  3. enhance the uses of standard ROM calles.  None of the routines are very complex
  4. but they are useful and have a standard interface that was thought out with
  5. some care.  I hope you find them useful.
  6.  
  7. unit Utilities;
  8.  
  9. interface
  10.  
  11.     const
  12.         {Useful MacConstants}
  13.         DefaultItem   = 1;
  14.         Margin5       = 5;
  15.         Margin16      = 16;
  16.         ScrollBarSize = 16;
  17.         Window_Title  = 18;
  18.         MenuBarSize   = 20;
  19.         NewRomsID     = 117;
  20.         DeadControl   = 255;
  21.         TESelStart    = 0;
  22.         TESelEnd      = 32767;
  23.         TooMuch       = 16777216;
  24.         
  25.         {Useful Mac Key code constants.  ASCII codes}
  26.         ETX                    = $03;    {Enter Key}
  27.         EnterKey        = ETX;
  28.         BS                    = $08;    {BackSpace Key}
  29.         CR                    = $0D;    {Return Key}
  30.         ReturnKey        = CR;
  31.         HT                    = $09;    {TAB Key}
  32.         TabKey            =    HT;
  33.         ESC                    =    $1B;    {Escape Key}
  34.         FS                    =    $1C;    {Left arrow key}
  35.         LeftArrow        =    FS;
  36.         GS                    =    $1D;    {Right arrow key}
  37.         RightArrow    = GS;
  38.         RS                    =    $1E;    {Up arrow key}
  39.         UpArrow            =    RS;
  40.         US                    =    $1F;    {Down arrow key}
  41.         DownArrow        = US;
  42.         EndofLine        = $7F;    {End of Line Delimeter}
  43.         NoBrkSpace    = $CA;    {None Breaking space for Mono Spacing}
  44.  
  45.       {Utility Dialog Constants}
  46.       YesNoCancelID = 1966;
  47.           YesButton         = 1;
  48.           CancelButton    =    2;
  49.           NoButton            =    3;
  50.           StaticText        =    4;
  51.           
  52.     type
  53.         GenericResponse = (nullResponse, YesResponse, CancelResponse, NoResponse);
  54.  
  55.     var
  56.       theDialogFilter    : ProcPtr;    {A Pointer to the Dialog Filter Procedure}
  57.       ScreenSize            :    Rect;            {A Global constant containing the screen size}
  58.  
  59.     procedure StandardInit;
  60.  
  61.     function GetEven (theNum: LONGINT): LONGINT;
  62.     function GetRectSize (theRect: Rect): Point;
  63.     function NewRoms: BOOLEAN;
  64.     function HFSRunning: BOOLEAN;
  65.     function OptKeyDown     (theModifiers: INTEGER): BOOLEAN;
  66.     function CapsLockDown (theModifiers: INTEGER): BOOLEAN;
  67.     function ShiftKeyDown    (theModifiers: INTEGER): BOOLEAN;
  68.     function CmdKeyDown     (theModifiers: INTEGER): BOOLEAN;
  69.     function WindowActivate (theModifiers: INTEGER): BOOLEAN;
  70.  
  71.     procedure CenterInRect (VAR theRect: Rect; theCenterRect: Rect;
  72.                             vOffSet, hOffSet: INTEGER);
  73.     procedure CenterInScreen (VAR theRect: Rect; vOffSet, hOffSet: INTEGER);
  74.     
  75.     function GetWindowKind (theWindow: WindowPtr): INTEGER;
  76.     function FrontSysWindow: BOOLEAN;
  77.     function CenterNewWindow (theWindowID: INTEGER; wStorage: Ptr;
  78.                               behind: WindowPtr; vOffSet, hOffSet: INTEGER): WindowPtr;
  79.  
  80.     procedure ToggleButton (theControl: ControlHandle);
  81.     
  82.   function  DoSFGet (theFileCount: INTEGER; theTypeList: SFTypeList;
  83.                       vOffSet, hOffSet: INTEGER): SFReply;
  84.     function  DoSFPut (theSavePrompt, OrigFileName: str255;
  85.                         vOffSet, hOffSet: INTEGER): SFReply;
  86.  
  87.     function CenterNewDialog (theDialogID: INTEGER; dStorage: Ptr;
  88.                               behind: WindowPtr; vOffSet, hOffSet: INTEGER): DialogPtr;
  89.     procedure OutLineButton (thePort: GrafPtr; theRect: Rect);
  90.     procedure DialogDefault (theDialog: DialogPtr);
  91.     function  DialogFilter (theDialog: DialogPtr; VAR theEvent: EventRecord;
  92.                                                    VAR theItem: INTEGER): BOOLEAN;
  93.     procedure SetupDialog (theDialog: DialogPtr;
  94.                             ButtonKeys, CheckBoxKeys, RadioKeys: BOOLEAN);
  95.     procedure DisposSetUp (theDialog: DialogPtr);
  96.     function GetDLOGRefCon (theDialog: DialogPtr): LONGINT;
  97.     function YesNoCancelDLOG (parm0, parm1, parm2, parm3: str255): GenericResponse;
  98.  
  99.  
  100. Instructions:::
  101.  
  102. Constants:
  103.     The constants can be used any way that the programmer sees fit.  Here is a list
  104. of some of the more useful ones for Macintosh Programming.
  105.  
  106. DefaultItem:    this is simply the number one but that is also the Item that
  107.                             modalDialog returns when the return or enter key is hit. Therefore
  108.                             this is useful for comparisions after calling ModalDialog.
  109. ScrollBarSize:    This constant is the number of pixels wide (or tall) of a ScrollBar
  110. Window_Title:    This is the height (in pixels) of a standard window title bar.
  111. MenuBarSize:    This is the height (in pixels) of the standard Menu Bar.
  112. DeadControl:    This is the value you pass to HiliteControl to disable a control.
  113. TESelStart,
  114. TESelEnd:    This two constants together will select all of the text in any 
  115.                     TE Edit record when passed in the proper parameters.
  116. YesNoCancelID:    This is the resource ID of the Yes/No/Cancel Dialog provided by
  117.                                 the Utility Unit.
  118.  
  119. StandardInit:
  120.     This procedure calls ALL possible Init procedures in the Macintosh.  In
  121. addition to calling the init routines it installs a procedure that enables the
  122. resume button in the system error box that will take you back to the Finder.
  123. NOTE:    This init procedure assumes you are running under System 3.2 and
  124.             Finder 5.3 or later
  125.  
  126. GetEven:
  127.     Given a LONGINT this function returns the NEXT even number if the number is
  128. ODD.  This is useful for macintosh programing since all addresses have to be
  129. on even word boundries.
  130.  
  131. GetRectSize:
  132.     Given a rectangle this function returns the width and height of the rectangle
  133. in a Point.  Where Point.v = height and Point.h = width.
  134.  
  135. NewRoms:
  136.     If this function returns true you are currently running on a machine that has
  137. the New 128K ROMS installed.
  138.  
  139. HFSRunning:
  140.     If this function returns true you are currently running on a machine that has
  141. the HFS filing system currently active.
  142. NOTE: This doesn't mean you also have the new ROMS since HFS can be loaded into
  143.             RAM at boot time.
  144.  
  145. OptKeyDown,
  146. CapsLockDown,
  147. ShiftKeyDown,
  148. CmdKeyDown,
  149. WindowActivate:
  150.     Given the modifiers field from an eventRecord this routines return TRUE if
  151. the Keys that are abreviated where down when the event was posted.  In the case
  152. of WindowActivate the routine returns TRUE if the Activate bit was set by an
  153. Update event.
  154.  
  155. CenterInRect:
  156.     Given two rectangles and a vertical and horizontal offset, this routine will
  157. center the first rectangle in the second rectangle, and then offset the
  158. now centered first rectangle by the requested amount.  The centered and then
  159. offset rectangle is then passed backout in the first parameter.
  160.     The offsets can be either positive or negitive.  Two positive parameters
  161. indicate an offset down and to the right, negitive ='s up and to the left. This
  162. is in keeping with the way quickdraw does it's relative offsets.  If no offset
  163. is desired simply pass 0 as the arguments.
  164. NOTE: This routine was written assuming that the first rectangle would be 
  165.             smaller than the second rectangle.  The results will be wrong if this
  166.             is not true.
  167.  
  168. CenterInScreen:
  169.     Given a rectangle and a vertical and horizontal offset this routine behaves
  170. exactly like the one above except it will automaticly center the rectangle in
  171. the current screen without the MenuBar.  The rectangle will not be
  172. centered in the screen, but it will be centered in the screen between the 
  173. bottom of the menubar to the bottom of the screen.  The normal desktop region
  174. that is the gray pattern.
  175.  
  176. GetWindowKind:
  177.     Given a windowPtr this routine returns the windowKind field of the windowrecord
  178. This is useful for checking if a wind is a deskaccessory.
  179.  
  180. FrontSysWindow:
  181.     This function returns TRUE if the frontmost window is a Desk Accessory.
  182.  
  183. CenterNewWindow:
  184.     This function is exactly the same a GetNewWindow, except it also centers the
  185. window, (by calling CenterInScreen), and then shows it.  Show if you window template
  186. was invisible this routine would recall it from the resource file, center it
  187. and then show it.
  188.     I have added two additional parameters to this function that are the vertical
  189. and horizontal offsets for after the window is centered.
  190. NOTE:    This routine uses the window's portrect as the rectangle that is centered
  191.             this is fine for most windows but not a standard document window.  The
  192.             portrect of a standard document window doesn't include the title bar size
  193.             as part of the portrect.  So even thought the portrect will be centered
  194.             the window itself might not be.  If this is a problem simply pass the
  195.             constant Window_Title as the vertical offset.
  196.  
  197. ToggleButton:
  198.     Given a handle to a control this routine toggles the hilighting between one and
  199. zero.  If it's one it changes to zero, and if it zero it changes it to one.
  200. This routine is particulary useful for turning radio buttons and checkboxes on
  201. and off, and makes it so the code doesn't have to keep track of the buttons
  202. hilite state.  Because if you call it will the last control it will turn it
  203. off and then call it with the new control and it will then turn that one on.
  204. All the programer has to do is keep track of the last control he turned on and
  205. the new control that needs to be turned on.
  206.  
  207. DoSFGet:
  208.     Given a file type count, the file type list and a vertical and horzontal
  209. offset.  This function automaticly puts up and centers the Standard File open
  210. Dialog and returns an SFReply that is the file choosen by the user.
  211.     For more information on the file type count and the file type list and the
  212. SFReply record see inside macintosh Vol. I chapter 20.
  213.  
  214. DoSFPut
  215.     Given a save file prompt and the original string and a vertical and horizontal
  216. offset.  This function automaticly puts up and centers the Standard File save
  217. Dialog and returns an SFReply that is the save file choosen by the user.
  218.     For more information on theSavePrompt, OrigFileName and SFReply record see
  219. inside macintosh Vol. I chapter 20.
  220.  
  221. CenterNewDialog:
  222.     This function is identical to GetNewDialog and it behaves exactly like
  223. CenterNewWindow, except that this routine is for Dialogs.  The parameters,
  224. (except for the vertical and horizontal offsets), are identical to the
  225. GetNewDialog parameters.
  226.     This function gets the dialog, centers it in the current screen and then
  227. shows it show if your dialog template is invisible the user won't see all of
  228. the movement on the screen.
  229.  
  230. OutLineButton:
  231.     Given a GrafPtr and a rectangle this routine outlines, or bolds, the rectangle
  232. with the standard bold outline for buttons.
  233.  
  234. DialogDefault:
  235.     Given a DialogPtr this routine automaticly outlines, or bolds, the default
  236. item with the standard macintosh bold outline.
  237.  
  238. DialogFilter:
  239.     This is a filter provided as a "new" standard filter procedure for Modal Dialogs
  240. After the neccessary setup procedures have been called, by passing a procedure pointer
  241. (theDialogFilter) to modal Dialog this filter will execute.
  242.     If the user hits return or enter the default Item is returned as hit, this
  243. is no different than the standard macintosh dialog filter.
  244.     If the user double clicks on a radio button the filter first returns the
  245. radio button item number and then returns the default item as hit if the second
  246. click was in the double click time period and the click occured on the same item.
  247. This is useful so the use can double click on a radio button and dismiss the
  248. Dialog in one simple double click.
  249.     It also montiors command-key events and maps them onto the the appropriate
  250. Item numbers.  You can have command key equivilants for buttons, check boxes,
  251. and radio buttons.  The first character in the buttons Title determines it's
  252. keyboard equivilant.  If the buttons title was "Red" the command-R would cause
  253. the filter procedure to return as though the mouse was clicked on the button
  254. red.
  255. NOTES:
  256.     1)    This routine uses some new ROM calls and will crash if executed on an old
  257.             machine.  But the Filter checks to see if it is on an old machine and
  258.             disables itself calling the standard ModalDialog filter procedure if it
  259.             is running on an old machine.
  260.     2)    The call to use this filter goes something like this
  261.  
  262.                 theDialog := GetNewDialog(...);
  263.                 SetUpDialog(theDialog, TRUE, TRUE, TRUE);  {more on this later}
  264.  
  265.                 while somecondition
  266.                     ModalDialog(theDialogFilter, ItemHIT);
  267.                     ....
  268.                     ....
  269.                     ....
  270.                 end;    {while somecondition}
  271.  
  272.                 DisposSetUp(theDialog);
  273.                 DisposDialog(theDialog);
  274.  
  275.             Notice the global variable theDialogFilter is used.  Please don't set
  276.             this value yourself, it is set for you every time SetupDialog is called
  277.             and is set properly depending on if the new ROMS are present.
  278.  
  279.             This call will work on all macintosh's, but on the older one the features
  280.             of this filter are simply disabled.
  281.  
  282.             Notice the SetUpDialog and DispoSetUp call, these calls are necessary to
  283.             setup some data structures for the filter to use.  DisposSetup simply
  284.             removes the temporary data structures created by SetupDialog.
  285.  
  286. SetUpDialog:
  287.     Call this procedure before calling modal dialog with myFilter.  The four
  288. parameters are as follows, the DialogPtr that is the dialog to be setup and
  289. the booleans indicating the type of buttons to have keyboard equivilants 
  290. assigned to them.  By passing true to these parameters tells the setup to 
  291. assign a keyboard equivilant to that type of button, false tells the procedure
  292. to ignore that type of button when setting up the keyboard map.
  293.     In the event that two, or more, buttons have the same first character in
  294. their titles only the first occurence is used and all others are ignored.
  295.  
  296. DisposSetUp:
  297.     Call this routine when you are done with theDialog and want to free up the
  298. memory allocated by SetupDialog.
  299.  
  300. GetDLOGRefCon:
  301.     I use the refCon to hold the handle pointing to my data structures so after
  302. calling SetUpDialog, you must you this call to retreive you RefCon that you
  303. stored in the Dialog.  After calling disposSetup the RefCon is restored and
  304. you can use the normal tool box calls to get at it.
  305.  
  306. YesNoCancelDLOG:
  307.     This displays the standard YES/NO/Cancel DLOG as outlined by Inside Macintosh
  308. Volume IV for use when asking the user a question such as
  309. "Save Changes Before Quitting".  It takes four parameters which determine the
  310. message that is displayed to the user and returns and value of type GenericResponse
  311. which you program can then respond to.
  312.     It automaticly centers the dialog, outlines the default response, and uses
  313. my filter for ModalDialog with keyboard equivilants for each of the three 
  314. buttons.  A sample call might be as follows
  315.  
  316.     FileName := 'Test Data';
  317.     ActionStr := 'Quitting??';
  318.  
  319.     {the message displayed to the user would be
  320.             'Save changes to Test Data before Quitting??'}
  321.  
  322.     theResponse := YesNoCancelDLOG('Save changes to ', FileName, ' before ', ActionStr);
  323.     case theResponse of
  324.         YesResponse : begin
  325.                                         DoSave(...);
  326.                                         Closewindow(...);
  327.                                         ...
  328.                                     end;
  329.         NoResponse : begin
  330.                                     CloseWindow(...);
  331.                                     ....
  332.                                     end.
  333.         CancelResponse: your choice of code
  334.     end;    {case}
  335.  
  336.     Have fun and I hope you find the utilities useful, I'm open to ideas and
  337. questions.
  338.  
  339. David O'Rourke